﻿using System.Linq;
using System.Security.Permissions;
using OS.Infrastructure.Domain;
using OS.Infrastructure.Domain.UnitOfWork;
using TaskKernel.Configuration;
using TaskKernel.Domain;
using TaskKernel.Domain.Interface;

namespace TaskWebApplicationUI.PresenterLayer
{
    public class AdminDepartmentsPresenter : AbstractPresenter<IAdminDepartmentsView>
    {
        private readonly IQueryDepartment _queryDepartment;
        private readonly IUnitOfWorkFactory _unitOfWork;
        private readonly ICommandRepository<Department> _command;

        public AdminDepartmentsPresenter(IQueryDepartment queryDepartment,
                                         IUnitOfWorkFactory unitOfWork, ICommandRepository<Department> command)
        {
            _queryDepartment = queryDepartment;
            _unitOfWork = unitOfWork;
            _command = command;
        }

        public void OnViewInitialized()
        {
            using (var z = _unitOfWork.Create())
            {
                var departments = _queryDepartment.GetAllDepartments().ToList();
                if (departments.Any()) View.Departments = departments;
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnUpdateDepartment(Department department)
        {
            using (var z = _unitOfWork.Create())
            {
                var changeDepartment = _queryDepartment.GetDepartment(department.UID);
                changeDepartment.MailBox = department.MailBox;
                changeDepartment.DepartmentName = department.DepartmentName;
                changeDepartment.WebPath = department.WebPath;
                changeDepartment.IsClaim = department.IsClaim;
                changeDepartment.IsClaimNotify = department.IsClaimNotify;
                changeDepartment.RatingRequired = department.RatingRequired;
                _command.Save(changeDepartment);
                if (z.Commit())
                    View.Message = "Изменения внесены";
            }
            OnViewInitialized();

        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnAddDepartment(Department department)
        {
            using (var z = _unitOfWork.Create())
            {
                _command.Save(department);
                if (z.Commit())
                    View.Message = "Новый отдел добавлен";
            }
            OnViewInitialized();
        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnDeleteDepartment(Department department)
        {
            using (var z = _unitOfWork.Create())
            {
                var delDepartment = _queryDepartment.GetDepartment(department.UID);
                if (!delDepartment.UsersWithRole.Any() && !delDepartment.Tasks.Any())
                {
                    _command.Delete(delDepartment);
                    if (z.Commit())
                        View.Message = "Отдел удален";
                }
                else
                    View.Message = "Нельзя удалить отдел, где есть пользователи с ролью и/или задачи";
            }
            OnViewInitialized();
        }
    }
}